var directlyShow = false;//�Ƿ�ֱ����ʾ���ʵĽ���
var showComplete = false;//�����Ƿ����ڶ�ʱ��ʾ������Ѿ���ʾ������û���û��ȡ������ʱ�����ֱ�Ӳ鿴���͡���Ч

$(document).ready(function(){
  // 1.Ϊ��һ������һ����ťע�����¼�
  $("#preWord").click(function(){
     //1.��ȡ�α�id��
     //2.��ȡƫ������
     var bookid = $("#bookid").val();
     var index =  parseInt($("#index").val())-1;
     var type = 1;
     if(index<0){
	 alert("�Ѿ��ǵ�һ������");
     }
     getWord(bookid,index,type);
  });

  $("#nextWord").click(function(){
     //1.��ȡ�α�id��
     //2.��ȡƫ������
     var bookid = $("#bookid").val();
     var index = parseInt($("#index").val())+1;
     var type = 0;
     getWord(bookid,index,type);
  });


   //ֱ����ʾ����
   $("#directlyShow").click(function(){
      if(!showComplete){
	 directlyShow = true;
      }
      
   });

  //����
  $("#recitation").click(function(){
    var spelling = $("#word").html();
    if(spelling!= ""||selling.length!=0){
       pronunciation(spelling);
    }
    
  });

  //��Ϊ��Ϥ
  $("#familiar").click(function(){
    var bookid = $("#bookid").val();
    var wordid = $("#wordid").val();
    var level = 3;
    setLevel(level,wordid,bookid);
    
  });
   
   //��Ϊİ��
   $("#strange").click(function(){
    var bookid = $("#bookid").val();
    var wordid = $("#wordid").val();
    var level = 1;
    setLevel(level,wordid,bookid);
    
   });

   //��Ϊİ��
   $("#fuzzy").click(function(){
    var bookid = $("#bookid").val();
    var wordid = $("#wordid").val();
    var level = 2;
    setLevel(level,wordid,bookid);
    
   });


   var pid = "word_0_0";
   getBookList(pid);
   
});

function getBookList(pid,flag){
    $.get("../jsonepowerbook/listEpowerBookByPid.action?pid="+pid,null,function(response){
         if(response.resultCode!=1){
		 }
         var bookList= response.result.bookList;
		 $("#right1").html(bookList);
		 alert(bookList);
		 loadDomClick();//初始化dom事件
	});
}

function loadDomClick(){
            var id="right1";
			var tag="em";
			for ( var _obj = document.getElementById(id).getElementsByTagName(tag), i = -1, em; em = _obj[++i];) {
				em.onclick = function() { //onmouseover 
					var ul = this.nextSibling;
					if (!ul) {
						return false;
					}
					ul = ul.nextSibling;
					if (!ul) {
						return false;
					}
					if (tag != 'a') {
						ul = ul.nextSibling;
						if (!ul) {
							//动态获取dom
							var pid = this.id;
							if(pid.split("_")[1]==5){
							  return false;
							}
							var id = this.parentNode.id;
					         $.get("../jsonepowerbook/listEpowerBookByPid.action?pid="+pid,null,function(response){
                                   if(response.resultCode!=1){
									   alert("error le ......");
		                           }
                                   var bookList= response.result.bookList;
		                           alert("id"+id);
                                   $('#'+id).append(bookList);
		                           loadDomClick();//初始化dom事件
		 					 });
							return false;
						}
					} //a 标签控制 隐藏或删除该行
					for ( var _li = this.parentNode.parentNode.childNodes, n = -1, li; li = _li[++n];) {
						if (li.tagName == "LI") {
							for ( var _ul = li.childNodes, t = -1, $ul; $ul = _ul[++t];) {
								switch ($ul.tagName) {
								case "UL":
									$ul.className = $ul != ul ? ""
											: ul.className ? "" : "off";
									break;
								case "EM":
									$ul.className = $ul != this ? ""
											: this.className ? "" : "off";
									break;
								}
							}
						}
					}
				}
			}

}

 function loadBookValue(){
         var id=this.id;
         $("bookid").val(id);
         var bookid = id;
         var index = parseInt($("#index").val())+1;
         var type = 0;
         getWord(bookid,index,type);
}


function getWord(bookid,index,type){
     $.get("../jsonepowerword/getEpowerWord.action?bookid="+bookid+"&index="+index+"&type="+type,null,function(response){
	  if(response.resultCode==-3){
             alert("get word fail");
	     return;
	  }
	  if(response.resultCode==10){
             alert("�Ѿ��Ǳ������һ�����һ������");
	     return;
	  }
	  var sentence= response.result.word.sentence;
	  var explain = response.result.word.meaning;
	  var word =  response.result.word.spelling;
	  var wordid = response.result.word.wordid;
	  var newIndex = response.result.index;
	  $("#word").html(word);
	  $("#wordid").val(wordid);
	  $("#time").html("5");
	  $("#time").show();
      $("#index").val(parseInt(newIndex));
	  showComplete = false;
	  //��ʱ����5s����ʾ
	 var time = 5;
	 var show = false;//��ʾ�Ƿ���ʾ
         $(this).everyTime('1s', function() {
            if(show){//����Ѿ���ʾ�����治��Ҫ��ִ��
	       return;
	    }
	    if(directlyShow){
              $("#time").hide();
	      $("#sentence").html(sentence);
	      $("#explain").html(explain);

	      $("#preWord").removeAttr("disabled");
	      $("#nextWord").removeAttr("disabled");
	      directlyShow = false;
	      show = true;
	      showComplete = true;
	    }else{
	       time = time-1;
	       if(time>0){
                  $("#time").html(time);
	          //TODO ����һ��,��һ����ť��Ϊ������
	          $("#preWord").attr("disabled","disabled");
	          $("#nextWord").attr("disabled","disabled");
		
	        }else{
	          $("#time").hide();
	          $("#sentence").html(sentence);
	          $("#explain").html(explain);
		  
	          //TODO ����һ����һ����ť��Ϊ����
	          $("#preWord").removeAttr("disabled");
	          $("#nextWord").removeAttr("disabled");
		  showComplete = true;
	        }
	    }
         },5);
	
     });
}

function setLevel(level,wordid,bookid){
     $.get("../epoweruserword/resetUserWordLevel.action?level="+level+"&wordid="+wordid+"&bookid="+bookid,null,function(response){
	     if(response.resultCode==1){
	         alert("ok");
		 return;
	     }
	     alert("����ʧ��");
	 
     });
} 
